package com.example.gorannet;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.Toast;

@SuppressLint("SimpleDateFormat")
public class GCMmessages extends Fragment
{
	
	HashMap<String, HashMap<String, HashMap<String, String>>> hashMapDataMap;
	
	private ExpandableListAdapter			listAdapter;

	private ExpandableListView				expListView;

	private List<String>					listDataHeader;

	private HashMap<String, List<String>>	listDataChild;
	long									oms, nms;

	private HashMap<String, String>	hashMapChildOne;

	private HashMap<String, String>	hashMapChildTwo;

	private HashMap<String, HashMap<String, String>>	hashMapFatherOne;

	private HashMap<String, HashMap<String, String>>	hashMapFatherTwo;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.activity_main, container, false);
		List<String> today1 = new ArrayList<String>();
		DBHandler db = new DBHandler(getActivity());
		// try
		// {
		// today1 = db.getAllMessagesBetweenTimeframe(oms, nms);
		// String[] mStringArray = new String[today1.size()];
		// mStringArray = today1.toArray(mStringArray);
		// Toast.makeText(getActivity(), mStringArray[1],
		// Toast.LENGTH_LONG).show();
		// }
		// catch(Exception e)
		// {
		// Toast.makeText(getActivity(), "xalat", Toast.LENGTH_LONG).show();
		// }
		listDataChild = new HashMap<String, List<String>>();
		expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
		// preparing list data
		prepareListData();
		listAdapter = new ExpandableListAdapter(getActivity().getApplicationContext(), listDataHeader,
				listDataChild);
		// setting list adapter
		expListView.setAdapter(listAdapter);
		// Listview Group click listener
		expListView.setOnGroupClickListener(new OnGroupClickListener()
		{
			Context test = getActivity().getApplicationContext();
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id)
			{
				Toast.makeText(test ,
						"Group Clicked " + listDataHeader.get(groupPosition),
						Toast.LENGTH_SHORT).show();
				return false;
			}
		});

		// Listview on child click listener
		expListView.setOnChildClickListener(new OnChildClickListener()
		{
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id)
			{
				// TODO Auto-generated method stub
				Toast.makeText(
						getActivity(),
						listDataHeader.get(groupPosition)
								+ " : "
								+ listDataChild.get(
										listDataHeader.get(groupPosition)).get(
										childPosition), Toast.LENGTH_SHORT)
						.show();
				return false;
			}
		});
		return view;
	}

	private void prepareListData()
	{
		Calendar c = Calendar.getInstance();
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.add(c1.DAY_OF_YEAR, -1);
		c2.add(c2.DAY_OF_YEAR, -2);
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
		String date = df.format(c.getTime());
		String date1 = df.format(c1.getTime());
		String date2 = df.format(c2.getTime());
		String[] date01 = date.split(" ");
		String[] date11 = date1.split(" ");
		String[] date21 = date2.split(" ");
		// Toast.makeText(getApplicationContext(), date,
		// Toast.LENGTH_LONG).show();
		// Toast.makeText(getApplicationContext(), date1,
		// Toast.LENGTH_LONG).show();
		// Toast.makeText(getApplicationContext(), date2,
		// Toast.LENGTH_LONG).show();

		// Adding child data
		listDataHeader = new ArrayList<String>(3);

		listDataHeader.add( "Today");//date01[0]);
		listDataHeader.add("Yesterday");
		listDataHeader.add("Tow Days Ago");

		// Adding child data
		List<String> today = new ArrayList<String>();
		today.add("The Shawshank Redemption");
		today.add("The Godfather");
		today.add("The Godfather: Part II");
		today.add("Pulp Fiction");
		today.add("The Good, the Bad and the Ugly");
		today.add("The Dark Knight");
		today.add("12 Angry Men");

		List<String> yesterday = new ArrayList<String>();
		yesterday.add("The Conjuring");
		yesterday.add("Despicable Me 2");
		yesterday.add("Turbo");
		yesterday.add("Grown Ups 2");
		yesterday.add("Red 2");
		yesterday.add("The Wolverine");

		List<String> TowdaysAgo = new ArrayList<String>();
		TowdaysAgo.add("2 Guns");
		TowdaysAgo.add("The Smurfs 2");
		TowdaysAgo.add("The Spectacular Now");
		TowdaysAgo.add("The Canyons");
		TowdaysAgo.add("Europa Report");

		listDataChild.put(listDataHeader.get(0), today); // Header, Child data
		listDataChild.put(listDataHeader.get(1), yesterday);
		listDataChild.put(listDataHeader.get(2), TowdaysAgo);
	}
	
	public void getGCMData()
	{
		Calendar calendarNow = Calendar.getInstance();
		calendarNow.setTime(new Date(System.currentTimeMillis()));
		
		Calendar calendarStartDate = calendarNow;
		calendarStartDate.add(Calendar.DATE, -3); //-3 shall be a variable that is read from GCM settings
		
		hashMapChildOne = new HashMap<String, String>();
		hashMapChildOne.put("child1", "data1");
		hashMapChildOne.put("child2", "data2");
		hashMapChildOne.put("child3", "data3");
		
		hashMapChildTwo = new HashMap<String, String>();
		hashMapChildTwo.put("child4", "data4");
		hashMapChildTwo.put("child5", "data5");
		hashMapChildTwo.put("child6", "data6");
		
		hashMapFatherOne = new HashMap<String,HashMap<String, String>>();
		hashMapFatherOne.put("Father1", hashMapChildOne);
		
		hashMapFatherTwo = new HashMap<String,HashMap<String, String>>();
		hashMapFatherTwo.put("Father2", hashMapChildTwo);
		
		hashMapDataMap.put("01/01/2014", hashMapFatherOne);
		hashMapDataMap.put("02/01/2014", hashMapFatherOne);
	}
}
